﻿using Microsoft.Xna.Framework;

namespace XNAGuiLib.Core.Components
{
    public class Panel : GIComponent
    {

        #region Panel Public Field - Bounds
        /// <summary>
        /// gets or sets the bounds of the panel
        /// </summary>
        public new Vector2 Bounds
        {
            get { return new Vector2(this._width, this._height); }
            set { this.Resize((int)value.X, (int)value.Y); }
        }
        #endregion

        public Panel(Game game, int width, int height)
            : base(game)
        {
            this._width = width;
            this._height = height;
        }


        #region Panel Protected Method - Resize(int width, int height)
        /// <summary>
        /// resizes the panel and its children (scales them if autoscaling is set!)
        /// </summary>
        /// <param name="width">width to resize the panel to</param>
        /// <param name="height">height to resize the panel to</param>
        protected override void Resize(int width, int height)
        {
            foreach (GIComponent gi in this._gics)
            {
                float chld_ratio_width = (float)gi.Width / (float)this._width;
                float chld_ratio_height = (float)gi.Height / (float)this._height;

                float giWidth = gi.Bounds.X;
                if (gi.AutoScaleWidth)
                    giWidth = width  * chld_ratio_width;

                float giHeight = gi.Bounds.Y;
                if (gi.AutoScaleHeight)
                    giHeight = height * chld_ratio_height;

                gi.Bounds = new Vector2(giWidth, giHeight);

            }

            this._width = width;
            this._height = height;
        }
        #endregion


        #region Panel Public Method - Add(GIComponent item)
        /// <summary>
        /// adds the component to the panel
        /// </summary>
        /// <param name="item">the component to add</param>
        public new void Add(GIComponent item)
        {
            item.Owner = this;
            base.Add(item);
        }
        #endregion

        #region Panel Public Method - Remove(GIComponent item)
        /// <summary>
        /// removes the component from the panel
        /// </summary>
        /// <param name="item">the item to remove from the panel</param>
        /// <returns>true if panel was removed, false otherwise</returns>
        public new bool Remove(GIComponent item)
        {
            return base.Remove(item);
        }
        #endregion

        #region Panel Public Method - UnbindGIC(int index)
        /// <summary>
        /// unbinds/frees the component at index in list from the panel
        /// </summary>
        /// <param name="index">index of the component in list</param>
        /// <returns>the component which was unbound</returns>
        public new GIComponent UnbindGIC(int index)
        {
            return base.UnbindGIC(index);
        }
        #endregion


    }
}